<?php
/**
 * (logic)（临时）第三方发货费
 */
namespace App\Logic\File\ProfitLT;

use App\Models\MonthlyReportLT\Tmp\TmpThFee;
class ParseThFeeFile
{
    /**
     * 解析ThFeeFile
     * @param $date
     * @param $lines
     * @return int
     */
    public static function exeParse($lines,$date): int
    {
        $count = count($lines);
        $i = 0;
        $num = 100;
        $re_count = 0;
        while(true){
            if(empty($lines))break;
            $_lines = array_splice($lines,$i,$num);
            foreach ($_lines as $line){
                $flag = self::_addThFee($line,$date);
                $flag && $re_count++;
            }
        }
        return $re_count == $count ? 1 : 0;
    }

    private static function _addThFee($temp,$date): bool
    {
        if(count($temp) < 3)return true;
        if(!$temp[0])return true;

        //第三方发货费（临时）
        $re = TmpThFee::query()->create([
            'report_date' => $date,
            'order_id' => $temp[0],
            'sku' => $temp[1],
            'total' => $temp[2],
        ]);
        return (bool)$re;
    }
}
